package com.design_patterns.singleton;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-08-23 15:58
 **/

/**
 * 静态内部类
 */
public class StaticClass {
    private StaticClass() {

    }

    /**
     * 加载外部类不会加载内部类
     * 需要使用的时候调用内部类,完成初始化,实现按需加载,
     * 线程安全,JVM 保证,加载Class只加载一次
     */
    private static class StaticClassHolder {
        private final static StaticClass INSTANCE = new StaticClass();
    }

    public static StaticClass getInstance() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return StaticClassHolder.INSTANCE;
    }


    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.err.println(StaticClass.getInstance().hashCode());
            }).start();
        }
    }

}
